APM.ConfigHistoryController = Ember.Controller.extend({
	form: {
		cleanTime:"",
		confType:"10"
	},

	//列表数据存放数组
 	gridDataSource:  Ember.A(),

 	gridColumns: [
		{
			headerText: "",
			dataKey:"id",
			width: 45,
			allowSort: false,
			selectAll: true,
			ensurePxWidth: true,
			cellFormatter: function (args, colume) {
				if (args.row.type & $.wijmo.wijgrid.rowType.data) {
					var ctrl = this.get("controller");
					var data = args.row.data;
					var cb = $("<input type='checkbox' class='grid-checkbox'/>");

					cb.prop("checked", data._checked)
						.appendTo(args.$container)
						.data("model", data);
		
					data._$checkbox = cb;
					args.$container
						.on("click", ":checkbox", ctrl.networkCheckboxClick);
					//将数据的_checked属性与checkbox的选中状态绑定
					Ember.addObserver(data, "_checked", ctrl.bindNetworkCheckedToCheckbox);
					return true;
				}
			}
		},
		{
			headerText: "业务系统",
			dataKey:"name",
			ensurePxWidth: true
		}
	],
	
	initConfData: function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/systemConfig",{
			configItem:"history"
		}).then(function(response){
			var configobj=response.configData;
			if(configobj){
				if(configobj.history_cleantime){
					ctrl.set("form.cleanTime",configobj.history_cleantime);
				}
			}	
		});

		ctrl.store.ajax("GET", "configs/history").then(function(response){
			var diskobj=response.diskData;
			var diskDataArr=new Array();
			if(diskobj){
				var diskDatatmp=diskobj.nodes;
				var diskDataObj={};
				if(diskDatatmp.length>0){
					for(var k=0;k<diskDatatmp.length;k++){
						var name=diskDatatmp[k].address;
						var freeData=parseInt((((diskDatatmp[k].diskFree)/1024)/1024)/1024);
						var useData =parseFloat(new Number(((diskDatatmp[k].storeSize)/1024)/1024/1024).toFixed(2));
						var allData =parseInt((((diskDatatmp[k].diskTotal)/1024)/1024/1024));
						var otherData=parseFloat((allData-useData-freeData).toFixed(2));
						if(name){
							diskDataObj.name=name;
							diskDataObj.freeData=freeData;
							diskDataObj.useData=useData;
							diskDataObj.otherData=otherData;
						}
					}
					diskDataArr.push(diskDataObj);


					ctrl.set("diskDataArr", diskDataArr);
				}
			}
		});


	},

	diskDataGroupArr: Ember.computed('diskDataArr', function() {
		if (arguments.length == 1) {
			var group = [],
				diskDataArr = this.get('diskDataArr');

			if (diskDataArr) {
				for (var i = 0, len = Math.floor(diskDataArr.length / 2); i < len; i++) {
					group.push(Ember.A(diskDataArr.splice(0, 2)));
				}

				diskDataArr[0] && group.push(Ember.A(diskDataArr.splice(0, 2)));
			}
		}
		return Ember.A(group);
	}),
	    	            	
	checkFormValid: function(){
		var form = this.get("form");

		this.set("saveResult", null);
		
		if( form.cleanTime == "" ){
			this.set("formTip", "清理时间不能为空.");
			return false;
		}
		if(form.cleanTime){
			var reg = new RegExp("^[0-9]*$"); 
			if(!reg.test(form.cleanTime)){  
			   this.set("formTip", "清理时间为正整数!");
			   return false;
			}  
		}
		this.set("formTip", null);
		return true;
	},

	networkCheckboxClick: function(e) {
		e.stopPropagation();

		var cb = $(this);
		var model = cb.data("model");
		var checked = cb.prop("checked"); 

		cb.prop("checked", checked);
		Ember.set(model, "_checked", checked);
	},

	bindNetworkCheckedToCheckbox: function() {
		var data = this,
			cb = data._$checkbox;
		var checked = Ember.get(data, "_checked");
		cb.prop("checked", checked);
	},

	getappIds : function() {
		return Ember.A(this.get("gridDataSource").filterBy("_checked", true)).mapBy("id");
	},

	actions: {
		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},
		
		save: function(){
			if( !this.checkFormValid() ){
				return;
			}
			var ctrl = this;
			var form=ctrl.get("form");
			ctrl.set("saveResult", null);
			this.store.ajax("POST", "configs/systemConfig", {
				cleanTime:form.cleanTime,
				confType:form.confType
			}).then(function(response){
				alertTip("保存成功！");
			});
		},
		
		cleanData: function(){
			var ctrl = this;
			this.set("showImg",false);
			
			var form = ctrl.get("form");
			ctrl.store.ajax("GET", "applications")
			.then(function(response){
				var apps = Ember.A(response.applications||[]);

				ctrl.set('gridDataSource', apps);
				ctrl.set('form.myCleanTime', form.cleanTime)

				var dialog = Broada.getView("clean_history");
				dialog.openDialog();
			});
		},
		
		hisBackup: function(){
			var ctrl = this;
			ctrl.set("saveResult", null);
			
			confirmDialog("确定立即备份历史数据吗?")
			.then(function(){
				ctrl.store.ajax("PUT", "configs/history").then(function(response){
					alertTip("备份成功！");
				});
			});
		},

		cleanHistory: function(){
			var ctrl = this;
			
			var form = ctrl.get("form");

			ctrl.set("saveResult", null);

			var appIds = Ember.A(this.getappIds()||[]);

			if(appIds.length == 0){
				this.set("cleanHistoryTip", "请选择要清除的业务系统!");
				return;
			}

			this.set("showImg",true);

			ctrl.store.ajax("DELETE", "configs/history", {
				cleanTime: form.myCleanTime,
				appIds: appIds.join()
			})
			.then(function(response){
				alertTip("清理成功！");
				ctrl.set("showImg",false);
				ctrl.set("cleanHistoryTip", null);
				Broada.getView("clean_history").closeDialog();
				ctrl.initConfData();
			});
		},

		closeDialog: function(){
			Broada.getView("clean_history").closeDialog();
		}
	}
});